package cyou.breathe.init;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 应用初始化后置处理器，防止被初始化多次
 * @author: breathe
 * @createTime: 2025-06-16
 */
@RequiredArgsConstructor
public class ApplicationContentPostProcessor implements ApplicationListener<ApplicationReadyEvent> {
    private final ApplicationContext applicationContext;

    /**
     * 保证spring event只执行一次
     */
    private final AtomicBoolean executeOnlyOnce = new AtomicBoolean(false);
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        if(!executeOnlyOnce.compareAndSet(false, true)) {
            return ;
        }
        applicationContext.publishEvent(new ApplicationInitializingEvent(this));
    }
}